﻿#If NETFX_CORE Then
Imports Windows.Storage
Imports System.IO
Imports Runa.Common.Helper

Namespace Global.System.IO

    Public NotInheritable Class Directory

        Public Shared Function Exists(ByVal fullName As String) As Boolean
            Try
                Dim dir = StorageFolder.GetFolderFromPathAsync(fullName).GetResults()
                Return dir IsNot Nothing
            Catch ex As Exception
                Return False
            End Try
        End Function

        Public Shared Function CreateDirectory(ByVal fullName As String) As String
            fullName = GetNomalizedPath(fullName)
            Dim parts = fullName.Split("/")
            Dim MakePath =
                Function(i As Integer)
                    Return parts.Take(i + 1).ToList().ToString(Function(item) item.ToString(), "/", "", "")
                End Function
            For i As Integer = 0 To parts.Length - 1
                Dim currentPath = MakePath(i)
                If Exists(currentPath) = False Then
                    If Path.IsPathRooted(currentPath) Then
                        Throw New ArgumentException("Invalid path.")
                    End If
                    Dim dir = StorageFolder.GetFolderFromPathAsync(MakePath(i - 1)).GetResults()
                    dir.CreateFolderAsync(parts(i)).GetResults()
                End If
            Next
            If Exists(fullName) Then
                Return fullName
            Else
                Throw New IOException(String.Format("Failed to create the given directory {0}.", fullName))
            End If
        End Function

    End Class

End Namespace

#End If